package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.ConcreteAdmixtureFactEntity;
import com.sgb.kjwl.model.entity.ConcreteEntity;
import com.sgb.kjwl.model.entity.ConcreteStrongFactEntity;
import com.sgb.kjwl.view.ui.adapter.self.publish.ConcreteAdapter;
import com.sgb.kjwl.view.ui.adapter.self.publish.ConcreteSelectAdapter;
import com.sgb.kjwl.view.ui.adapter.self.publish.ConcreteServiceAdapter;
import com.sgb.kjwl.view.ui.adapter.self.publish.GoodsSpecificationsAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.ResolutionUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class GoodsSpecificationsPopWindow extends BasePopWindow {

    //强度等级
    private RecyclerView mStrongList;
    //服务 view
    private RecyclerView mServicRelerView;
    //配置 view
    private RecyclerView mDeployRecyclerView;
    //已选择 view
    private RecyclerView mDSeclectRecyclerView;
    //名称
    private TextView mTvGoodsName;
    //图片
    private ImageView mImageGoods;
    //二三级 列表adapter
    private List<ConcreteAdapter> concreteAdapterList;
    private LinearLayout rootLayout;
    private GoodsSpecificationsAdapter goodsSpecificationsAdapter;
    private ConcreteServiceAdapter adapterService;
    private ConcreteServiceAdapter adapterDeoloy;
    private ConcreteSelectAdapter adapterSelect;
    private List<String> skillNo;
    private List<String> serviceList;
    private List<String> deoloyList;
    private List<String> selectList;
    private Map<String, String> selectMap;
    private ConcreteEntity concreteEntity;

    public GoodsSpecificationsPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_goods_specifications;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        rootLayout = holder.getPop().findViewById(R.id.root_layout);
        mTvGoodsName = holder.getPop().findViewById(R.id.tv_goods_name);
        mImageGoods = holder.getPop().findViewById(R.id.img_goods);

        mStrongList = holder.getPop().findViewById(R.id.strong_list);
        mServicRelerView = holder.getPop().findViewById(R.id.service_list);
        mDeployRecyclerView = holder.getPop().findViewById(R.id.deploy_list);
        mDSeclectRecyclerView = holder.getPop().findViewById(R.id.select_list);

        serviceList = new ArrayList<>();
        serviceList.add("自卸");
        serviceList.add("泵送");
        deoloyList = new ArrayList<>();
        deoloyList.add("二级配");
        deoloyList.add("一级配");
        selectList = new ArrayList<>();
        selectMap = new HashMap<>();
        selectMap.put("0", serviceList.get(0));
        selectMap.put("1", deoloyList.get(0));
        selectList = new ArrayList<String>(selectMap.values());

        //强度等级
        goodsSpecificationsAdapter = new GoodsSpecificationsAdapter(mContext, null);
        goodsSpecificationsAdapter.setItemSelect(true);
        goodsSpecificationsAdapter.setDefItem(-1);

        mStrongList.setLayoutManager(new GridLayoutManager(mContext, 3));
        mStrongList.setAdapter(goodsSpecificationsAdapter);
        goodsSpecificationsAdapter.setOnItemClick(new AdapterOnItemClick<ConcreteStrongFactEntity>() {
            @Override
            public void onItemClick(ConcreteStrongFactEntity concreteStrongFactEntity, int position) {
                selectMap.put("2", concreteStrongFactEntity.getStrongLevel());
                updateSelectData();
            }
        });

        //服务
        adapterService = new ConcreteServiceAdapter(mContext, null);
        adapterService.setItemSelect(true);
        adapterService.setDefItem(0);
        adapterService.setDatas(serviceList);
        mServicRelerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        mServicRelerView.setAdapter(adapterService);
        adapterService.setOnItemClick(new AdapterOnItemClick<String>() {
            @Override
            public void onItemClick(String s, int position) {
                selectMap.put("0", s);
                updateSelectData();
            }
        });

        //配置
        adapterDeoloy = new ConcreteServiceAdapter(mContext, null);
        adapterDeoloy.setItemSelect(true);
        adapterDeoloy.setDefItem(0);
        adapterDeoloy.setDatas(deoloyList);
        mDeployRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        mDeployRecyclerView.setAdapter(adapterDeoloy);
        adapterDeoloy.setOnItemClick(new AdapterOnItemClick<String>() {
            @Override
            public void onItemClick(String s, int position) {
                selectMap.put("1", s);
                updateSelectData();
            }
        });

        //已选择
        adapterSelect = new ConcreteSelectAdapter(mContext, null);
        adapterSelect.setItemSelect(false);
        adapterSelect.setDefItem(-1);
        adapterSelect.setDatas(selectList);
        mDSeclectRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        mDSeclectRecyclerView.setAdapter(adapterSelect);
        adapterSelect.setOnItemClick(new AdapterOnItemClick<String>() {
            @Override
            public void onItemClick(String s, int position) {

            }
        });
        adapterSelect.Idelete(position -> {

            if (position == 2 && goodsSpecificationsAdapter.getCheckDatas().size()!=0) {
                goodsSpecificationsAdapter.setDefItem(-1);
                goodsSpecificationsAdapter.resetCheckDatas();
                selectMap.remove(String.valueOf(position));
            } else if (position >= 2 && goodsSpecificationsAdapter.getCheckDatas().size() == 0) {
                for (int i = 0; i < concreteAdapterList.size(); i++) {
                    if (position == i + position) {
                        concreteAdapterList.get(i).setDefItem(-1);
                        selectMap.remove(String.valueOf(position));
                    }
                }
            }

            updateSelectData();
            /*for (int i = 0; i < concreteAdapterList.size(); i++) {
                if (position == i + position) {
                    concreteAdapterList.get(i).setDefItem(-1);
                }
            }*/
        });
    }

    public void setChildView(List<ConcreteAdmixtureFactEntity> concreteAdmixtureFactList) {
        rootLayout.removeAllViews();
        ConcreteAdmixtureFactEntity entity;
        concreteAdapterList = new ArrayList<>();
        for (int i = 0; i < concreteAdmixtureFactList.size(); i++) {
            entity = concreteAdmixtureFactList.get(i);
            addTitleView(entity.getAdmixtureName());
            addRecyclerView(entity.getSpecList(), i);
            addLine();
        }
    }

    private void addTitleView(String title) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        layoutParams.leftMargin = ResolutionUtil.dp2px(mContext, 15);
        layoutParams.topMargin = ResolutionUtil.dp2px(mContext, 15);
        layoutParams.bottomMargin = ResolutionUtil.dp2px(mContext, 15);
        TextView textView = new TextView(mContext);
        textView.setText(title);
        textView.setTextColor(mContext.getResources().getColor(R.color.color_333333));
        rootLayout.addView(textView, layoutParams);
    }

    private void addLine() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        TextView textView = new TextView(mContext);
        textView.setHeight(ResolutionUtil.dp2px(mContext, 0.7f));
        textView.setBackgroundColor(mContext.getResources().getColor(R.color.color_999999));
        rootLayout.addView(textView, layoutParams);
    }

    private void addRecyclerView(List<ConcreteAdmixtureFactEntity.SpecEntity> specList, int i) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ConcreteAdapter adapter = new ConcreteAdapter(mContext, specList);
        concreteAdapterList.add(adapter);
        adapter.setIndex(i + 3);
        adapter.setItemSelect(true);
        adapter.setDefItem(-1);
        MaxRecyclerView recyclerView = new MaxRecyclerView(mContext);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(adapter);
        recyclerView.setNestedScrollingEnabled(false);
        adapter.setDatas(specList);
        adapter.setOnItemClick(new AdapterOnItemClick<ConcreteAdmixtureFactEntity.SpecEntity>() {
            @Override
            public void onItemClick(ConcreteAdmixtureFactEntity.SpecEntity filterEntity, int position) {
                selectMap.put(String.valueOf(adapter.getIndex() + 3), filterEntity.getAdmixtureName());
                updateSelectData();
            }
        });
        rootLayout.addView(recyclerView, layoutParams);
    }

    public void setData(ConcreteEntity concreteEntity) {
        this.concreteEntity = concreteEntity;
        goodsSpecificationsAdapter.setDatas(concreteEntity.getConcreteStrongFactList());
        setChildView(concreteEntity.getConcreteAdmixtureFactList());
    }

    public void setData(String goodsName, String goodsImg) {
        Glide.with(mContext).load(goodsImg).into(mImageGoods);
        mTvGoodsName.setText(goodsName);
    }

    private void updateSelectData() {
        Set set = selectMap.keySet();
        Object[] arr = set.toArray();
        Arrays.sort(arr);
        selectList.clear();
        for (Object key : arr) {
            selectList.add(selectMap.get(key));
        }
        adapterSelect.setDatas(selectList);
    }
}
